<?php
/**
 * @desc 短信中心
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Cache;
use App\Rules\CheckMobile;
use Illuminate\Support\Facades\Validator;

class PushController extends Controller
{
	/**
	 * @desc 发送短信
	 * @date 2017-07-14 14:46:52
	 */
	function smsPost($mobile, $content){
		$requestUrl = config('sms.smshttpUrl');
		$post_data = array(
				'name'=>config('sms.smsaccount'),
				'pwd'=>config('sms.smspassword'),
				'mobile'=>$mobile,
				'content'=>$content,
				'stime'=>'',
				'sign'=>'',
				'type'=>'pt',
				'extno'=>''
		);
		$commonController = new CommonController();
		$return = $commonController->curl_post_http($requestUrl, $post_data);
	}
	
	/**
	 * @desc 获取手机验证码，验证手机号码合法性，
	 */
	public function mobileSecuriytCode(Request $request){
		
		$mobile = $request->get('mobile');
		$validator = Validator::make($request->all(), [
				'mobile' => ['required', new CheckMobile()]
		]);
		
		if ($validator->fails()) {
			return response()->json($validator->messages(), 200);
		}
		
		$minutes = 5;//验证码只保存5分钟
		$content = '【海燕社】您的验证码为%s，' . $minutes . '分钟内有效。';
		$message = '';
		
		//生成6位的验证码
		$securiytCode = Cache::remember('securiyCode_' . $mobile, $minutes, function(){
			$commonController = new CommonController();
			return $commonController->randomNumber(6);//生成6位字符
		});
			
		$content = sprintf($content,$securiytCode);

        $res = $this->smsPost($mobile, $content);
		if ($res != 0)
        {
            abort(500);
        }
		
		return response()->json([
				'status'=> '200',
				'msg'=>'发送成功',
//				'code'=>$securiytCode
		]);
	}
}
